﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace template_dll
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }
        private DataTable dataTable;
        private CurrencyManager cm;
        SqlConnection conn_rev;
        public string server = "";

        private void Form4_Load(object sender, EventArgs e)
        {
            conn_rev = new SqlConnection(@"Server=" + server + ";Database=revenue;Integrated Security=True");
            dateTimePicker1.Value = DateTime.Now.Date.AddDays(-1);
        }

        private void RevenueSel(DateTime dt)
        {
            bool fl = false;
            if (dataGridView1.DataSource != null)
            {
                cm = (CurrencyManager)dataGridView1.BindingContext[dataGridView1.DataSource];
                cm.SuspendBinding();
                fl = true;
            }

            SqlStorProc.sp_params[] par = new SqlStorProc.sp_params[1];
            par[0].name = "@date";
            par[0].value = dt.Date.ToString("yyyyMMdd");
            dataGridView1.DataSource = null;
            dataTable = null;

            dataTable = (new SqlStorProc()).SqlProcExecWithQueryOneTable(conn_rev, "pas_revenueSelLastMonth", 30, par);
            dataGridView1.DataSource = dataTable.DefaultView;
            dataGridView1.Sort(dataGridView1.Columns[2], ListSortDirection.Descending);

            if (fl) cm.ResumeBinding();
            else cm = (CurrencyManager)dataGridView1.BindingContext[dataGridView1.DataSource];
        }

        private void HideShowRows()
        {
            if (dataGridView1.Rows.Count > 0)
            {
                cm.SuspendBinding();
                foreach (DataGridViewRow r in dataGridView1.Rows)
                {
                    r.Visible = true;
                    r.DefaultCellStyle.BackColor = SystemColors.ControlLightLight;

                    if (!Regex.Match(Convert.ToString(dataGridView1[dataGridView1.SortedColumn.Name, r.Index].Value), textBox1.Text, RegexOptions.IgnoreCase).Success)
                        r.Visible = false;
                }
                cm.ResumeBinding();
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            HideShowRows();
        }

        private void textBox1_Enter(object sender, EventArgs e)
        {
            HideShowRows();
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            RevenueSel(dateTimePicker1.Value);
            HideShowRows();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                DialogResult dr = MessageBox.Show("", "Удаление строк", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    SqlStorProc.sp_params[] par = new SqlStorProc.sp_params[3];
                    par[0].name = "@date";
                    par[0].value = dataGridView1[0, e.RowIndex];
                    par[1].name = "@pharmcode";
                    par[1].value = dataGridView1[1, e.RowIndex];
                    par[2].name = "@typedoc";
                    par[2].value = dataGridView1[3, e.RowIndex];
                    dataGridView1.DataSource = null;
                    dataTable = null;

                    (new SqlStorProc()).SqlProcExecNonQuery(conn_rev, "pas_revenueSelLastMonth", 30, par);
                    RevenueSel(dateTimePicker1.Value);
                    HideShowRows();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
